package com.xyd.service.impl;

import com.xyd.entity.User;
import com.xyd.mapper.UserMapper;
import com.xyd.service.UserService;
import com.xyd.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * 类描述：
 *
 * @author 张吉哲
 * @date 2021/1/9 13:48
 */
public class UserServiceImpl implements UserService {

  @Override
  public boolean registerUser(User user) {
    return MybatisUtil.getSqlSession(true).getMapper(UserMapper.class).registerUser(user);
  }

  @Override
  public boolean isHasUsername(String username) {
    String name = MybatisUtil.getSqlSession().getMapper(UserMapper.class).isHasUserName(username);
    return name != null;
  }

  @Override
  public User loginUser(String username, String passWord) {
    return MybatisUtil.getSqlSession().getMapper(UserMapper.class).loginUser(username, passWord);
  }

  @Override
  public Integer getUserCount() {
    SqlSession sqlSession= MybatisUtil.getSqlSession();
    int count=sqlSession.getMapper(UserMapper.class).getUserCount();
    MybatisUtil.closeSqlSession(sqlSession);
    return count;
  }

  @Override
  public List<User> visitUser(int pageIndex, int pageSize) {
    SqlSession sqlSession= MybatisUtil.getSqlSession();
    List<User> list= sqlSession.getMapper(UserMapper.class).visitUser(pageIndex,pageSize);
    MybatisUtil.closeSqlSession(sqlSession);
    return list;
  }

  @Override
  public User queryUser(String euUserId) {
    SqlSession sqlSession= MybatisUtil.getSqlSession();
    User user= sqlSession.getMapper(UserMapper.class).queryUser(euUserId);
    MybatisUtil.closeSqlSession(sqlSession);
    return user;
  }

  @Override
  public int backupdateUser(User user) {
    SqlSession sqlSession=null;
    sqlSession= MybatisUtil.getSqlSession();
    int result=sqlSession.getMapper(UserMapper.class).backupdateUser(user);
    sqlSession.commit();
    MybatisUtil.closeSqlSession(sqlSession);
    return result;
  }

  @Override
  public int deleteVisitUser(String euUserId) {
    SqlSession sqlSession= MybatisUtil.getSqlSession();
    int result= sqlSession.getMapper(UserMapper.class).deleteVisitUser(euUserId);
    sqlSession.commit();
    MybatisUtil.closeSqlSession(sqlSession);
    return result;
  }
}
